package rosedb.gui;


import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import javax.imageio.ImageIO;


import javax.swing.JPanel;

import javax.swing.Timer;
import riulib.gui.imagelist.ImageListListener;
import rosedb.model.Logger;


/**
 *
 * @author riu
 */
public class DiaPreviewPanel extends JPanel implements MouseMotionListener, MouseListener, ActionListener, ImageListListener<File>{

    private boolean smallPreview;
    private boolean noImage = false;
    


    public DiaPreviewPanel(MainFrame gui, String name, int startImage, boolean smallPreview) {
        //super(gui, "Bildervorschau zu '"+name+"'");
        super(true);
        this.gui = gui;
        this.smallPreview = smallPreview;

        currentImage = startImage;

                
        //init(gui, name, startImage);

//        this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
//        //this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//        this.setVisible(true);

        addMouseMotionListener(this);
        addMouseListener(this);
        
        mousePosition = ThingWhereTheMouseIsOver.Nothing;
    }


    public void init(String name, int startImage){
        

        
        currentImage=startImage>=0&&startImage<gui.getOneRoseImageList().count() ? startImage : 0;

        if(!smallPreview){
            try{img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));}catch(Exception e){Logger.log(e);}
        }

        if(!smallPreview){
            setBackground(Color.black);
        }

        time.start();

        repaint();
    }

    public void simplePreview(int startImage){
        noImage = false;
        currentImage=startImage;
        
        if(startImage>=0 && startImage<gui.getOneRoseImageList().count()){
            try{img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));}catch(Exception e){Logger.log(e); Logger.log("Current Image: "+currentImage+"    img: "+img);}
        }else{
            img = null;
        }
        
        repaint();
    }

    public void simplePreview(Image i){
        currentImage=0;
        img = i;
        noImage = true;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        if(currentImage>=0&&(currentImage<gui.getOneRoseImageList().count() || noImage)  && (img!=null)){
            //g.drawImage(imgs.get(currentImage), 0, 0, this.getWidth(), this.getHeight(), this);
            //Image img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));

            
                if(img.getWidth(this)*getHeight()/img.getHeight(this) < getWidth()-getInsets().right-getInsets().left){
                     g.drawImage(img, (getWidth()-img.getWidth(this)*getHeight()/img.getHeight(this))/2, getInsets().top, img.getWidth(this)*getHeight()/img.getHeight(this), getHeight()-getInsets().top-getInsets().bottom, this);
                }else{
                     g.drawImage(img, getInsets().left, (getHeight()-(img.getHeight(this)*getWidth()/img.getWidth(this)-getInsets().top*2-getInsets().bottom*1))/2, getWidth()-getInsets().left-getInsets().right, img.getHeight(this)*getWidth()/img.getWidth(this)-getInsets().top-getInsets().bottom*2, this);
                }
            
            
                 //original
//                border[2] = 250;    // Width
//                border[3] = 100;    // Height
//                border[0] = getWidth()/2-border[2]/2;   // X
//                border[1] = getHeight()-50-border[3];   // Y


                border[2] = 250;    // Width
                border[3] = 100;    // Height
                border[0] = getWidth()/2-border[2]/2;   // X
                border[1] = getHeight()*7/8-border[3];   // Y

            if( mousePosition != ThingWhereTheMouseIsOver.Nothing){
                g.setColor(Color.white);
                g.drawRoundRect(border[0], border[1], border[2], border[3], 40, 40);
            


            //Play
            

            play[0][0] = border[0] + 20;                // xpoints[0]
            play[1][0] = border[1] + 20;                // ypoints[0]

            play[0][1] = border[0] + 20;                // xpoints[1]
            play[1][1] = border[1] + border[3] - 20;    // ypoints[1]

            play[0][2] = border[0] + 70;                // xpoints[2]
            play[1][2] = (play[1][0]+play[1][1])/2;     // ypoints[2]

            if(!isDiaPlaying){
                if(mousePosition == ThingWhereTheMouseIsOver.Play){
                    g.setColor(Color.yellow);
                }else{
                    g.setColor(Color.WHITE);
                }
                g.fillPolygon(play[0], play[1], play[2][0]);// n = 3
                g.setColor(Color.BLACK);
                g.drawPolygon(play[0], play[1], play[2][0]);// n = 3
            }

            //Pause
            
            pause[3] = 20;                           // w
            pause[4] = border[3] - 20*2;               // h
            pause[2] = border[1] + 20;               // y

            pause[0] = border[0] + 20/2;               // x1
            pause[1] = pause[0] + pause[3] + 10;     // x2

            if(isDiaPlaying){
                if(mousePosition == ThingWhereTheMouseIsOver.Pause){
                    g.setColor(Color.yellow);
                }else{
                    g.setColor(Color.WHITE);
                }
                g.fillRect(pause[0], pause[2], pause[3], pause[4]);
                g.fillRect(pause[1], pause[2], pause[3], pause[4]);
                g.setColor(Color.black);
                g.drawRect(pause[0], pause[2], pause[3], pause[4]);
                g.drawRect(pause[1], pause[2], pause[3], pause[4]);
            }

            
            //Previous


            previous[0][0] = pause[1] + pause[3] + 20;
            previous[1][0] = border[1] + border[3]/2;

            previous[0][1] = previous[0][0] + 20;
            previous[1][1] = pause[2];

            previous[0][2] = previous[0][1];
            previous[1][2] = previous[1][1] + 20;

            previous[0][3] = previous[0][2] + 20/2;
            previous[1][3] = previous[1][2];

            previous[0][4] = previous[0][3];
            previous[1][4] = previous[1][3] + 20;

            previous[0][5] = previous[0][2];
            previous[1][5] = previous[1][4];

            previous[0][6] = previous[0][1];
            previous[1][6] = pause[2] + pause[4];


            if(currentImage > 0){

                if(mousePosition == ThingWhereTheMouseIsOver.Previous){
                    g.setColor(Color.yellow);
                }else{
                    g.setColor(Color.WHITE);
                }
                g.fillPolygon(previous[0], previous[1], previous[0].length);
                g.setColor(Color.black);
                g.drawPolygon(previous[0], previous[1], previous[0].length);
            }


            //Next
            

                next[0][0] = previous[0][4] + 20;
                next[1][0] = previous[1][4];

                next[0][1] = next[0][0];
                next[1][1] = previous[1][3];

                next[0][2] = next[0][1] + 20/2;
                next[1][2] = next[1][1];

                next[0][3] = next[0][2];
                next[1][3] = pause[2];

                next[0][4] = next[0][3] + 20;
                next[1][4] = border[1] + border[3]/2;

                next[0][5] = next[0][3];
                next[1][5] = pause[2] + pause[4];

                next[0][6] = next[0][2];
                next[1][6] = next[1][2] + 20;

                
            if(currentImage < gui.getOneRoseImageList().count()-1){
                if(mousePosition == ThingWhereTheMouseIsOver.Next){
                    g.setColor(Color.yellow);
                }else{
                    g.setColor(Color.WHITE);
                }
                g.fillPolygon(next[0], next[1], next[0].length);
                g.setColor(Color.black);
                g.drawPolygon(next[0], next[1], next[0].length);
            }
            

            
            //Close
            if(mousePosition != ThingWhereTheMouseIsOver.Nothing){
                /*
                 *   1         3
                 * 0             4
                 *
                 *        2
                 *      B   5
                 *        8
                 *
                 * A             6
                 *   9         7
                 *
                 */
            
                close[0][0] = next[0][4] + 20;
                close[1][0] = border[1] + 20 + 20/2;

                close[0][1] = close[0][0] + 20/2;
                close[1][1] = border[1] + 20;

                close[0][2] = close[0][1] + 1 * 20;
                close[1][2] = close[1][1] + 20;

                close[0][3] = close[0][1] + 2 * 20;
                close[1][3] = close[1][1];

                close[0][4] = close[0][3] + 20/2;
                close[1][4] = close[1][0];

                close[0][5] = close[0][2] + 20/2;
                close[1][5] = close[1][2] + 20/2;

                close[0][6] = close[0][4];
                close[1][6] = border[1] + border[3] - 20 - 20/2;

                close[0][7] = close[0][3];
                close[1][7] = border[1] + border[3] - 20;

                close[0][8] = close[0][2];
                close[1][8] = close[1][2] + 20;

                close[0][9] = close[0][1];
                close[1][9] = close[1][7];

                close[0][10] = close[0][0];
                close[1][10] = close[1][6];

                close[0][11] = close[0][5] - 20;
                close[1][11] = close[1][5];


                if(mousePosition == ThingWhereTheMouseIsOver.Close){
                    g.setColor(Color.yellow);
                }else{
                    g.setColor(Color.WHITE);
                }
                g.fillPolygon(close[0], close[1], close[0].length);
                g.setColor(Color.black);
                g.drawPolygon(close[0], close[1], close[0].length);
                
            }

            }
            
        }
        
    }



    public boolean isDiaPlaying(){
        return isDiaPlaying;
    }




    @Override
    public void mouseDragged(MouseEvent e) {
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mousePosition = ThingWhereTheMouseIsOver.Nothing;

        if(e.getX()>border[0] && e.getX() < border[0]+border[2] && e.getY() > border[1] && e.getY() < border[1]+border[3]){
            mousePosition = ThingWhereTheMouseIsOver.Panel;


            if(e.getX() < previous[0][0]){
                if(isDiaPlaying){
                    if(e.getX() > pause[0] && e.getX() < pause[1]+pause[3] && e.getY() > pause[2] && e.getY() < pause[2]+pause[4]){
                        mousePosition = ThingWhereTheMouseIsOver.Pause;
                    }
                }else{
                    if(e.getX() > play[0][0] && e.getX() < play[0][2] && e.getY() > play[1][0] && e.getY() < play[1][1]){
                        mousePosition = ThingWhereTheMouseIsOver.Play;
                    }
                }
            }else if(e.getX() < close[0][0]){
                if(e.getX() > previous[0][0] && e.getX() < previous[0][3] && e.getY() > previous[1][1] && e.getY() < previous[1][6]){
                    mousePosition = ThingWhereTheMouseIsOver.Previous;
                }else if(e.getX() > next[0][0] && e.getX() < next[0][4] && e.getY() > next[1][3] && e.getY() < next[1][5]){
                    mousePosition = ThingWhereTheMouseIsOver.Next;
                }
            }else if(e.getX() < border[0]+border[2]){
                if(e.getX() > close[0][1] && e.getX() < close[0][3] && e.getY() > close[1][0] && e.getY() < close[1][6]){
                    mousePosition = ThingWhereTheMouseIsOver.Close;
                }
            }


            
        }
                    

        if(smallPreview){
            mousePosition = ThingWhereTheMouseIsOver.Nothing;
        }


        if(mousePosition != ThingWhereTheMouseIsOver.Nothing){
            setCursor(handCursor);
        }else{
            setCursor(Cursor.getDefaultCursor());
        }
        repaint();
    }

    private Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);

    @Override
    public void mouseClicked(MouseEvent e) {
        if(smallPreview){
            if(e.getClickCount()>=2 && gui.getOneRoseImageList().count()>0){
                DiaPreviewPanel p = new DiaPreviewPanel(gui, "", currentImage, false);
                new DiaPreviewWindow(p);    
                p.init("", currentImage);
            }
        }else{
            if(mousePosition == ThingWhereTheMouseIsOver.Play){
                isDiaPlaying = true;
            }else if(mousePosition == ThingWhereTheMouseIsOver.Pause){
                isDiaPlaying =  false;
            }else if(mousePosition == ThingWhereTheMouseIsOver.Previous){
                currentImage = currentImage > 0 ? currentImage-1 : currentImage;
                try{img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));}catch(Exception ex){}
                repaint();
            }else if(mousePosition == ThingWhereTheMouseIsOver.Next){
                currentImage = currentImage < gui.getOneRoseImageList().count()-1 ? currentImage+1 : currentImage;
                try{img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));}catch(Exception ex){}
                repaint();
            }else if(mousePosition == ThingWhereTheMouseIsOver.Close){
     //            this.dispose();
                if(parent != null){
                    parent.dispose();
                }

            }

            if(mousePosition != ThingWhereTheMouseIsOver.Close){
                mouseMoved(e);
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // Skipp Pic
        if(isDiaPlaying){
            if(currentImage < gui.getOneRoseImageList().count()-1){
                currentImage++;
            }else{
                currentImage = 0;
            }

            try{img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));}catch(Exception ex){}
            repaint();
        }

    }

    public void setparent(DiaPreviewWindow p){
        parent = p;
    }

    @Override
    protected void finalize() throws Throwable {
        gui = null;
        time.stop();
        time = null;
        super.finalize();
        
    }

    private Image img;


    
    private int currentImage;
    
    private MainFrame gui;
    

    /**
     * 0:   X<br>
     * 1:   Y<br>
     * 2:   Width<br>
     * 3:   Height<br>
     */
    private int[] border = new int[4];

    /**
     * 0:   xpoints[3]<br>
     * 1:   ypoints[3]<br>
     * 2:   n[1]<br>
     */
    private int[][] play = new int[][]{new int[3], new int[3], new int[]{3}};

    /**
     * 0:   x1<br>
     * 1:   x2<br>
     * 2:   y<br>
     * 3:   w<br>
     * 4:   h<br>
     */
    private int[] pause = new int[5];




    /**
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;\<br>
     *&nbsp;1--2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<br>
     *&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4<br>
     *&nbsp;0--6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;/<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5<br>
     */
    private int[][] next = new int[2][7];

    /**
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;|<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;2--3<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\&nbsp;&nbsp;&nbsp;5--4<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\&nbsp;|<br>
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6<br>
     */
    private int[][] previous = new int[2][7];


    /**
     *   1         3
     * 0             4
     *
     *        2
     *      B   5
     *        8
     * 
     * A             6
     *   9         7
     *
     */
    private int[][] close = new int[2][12];

    private boolean isDiaPlaying = false;
    private ThingWhereTheMouseIsOver mousePosition = ThingWhereTheMouseIsOver.Panel;
    private Timer time = new Timer(5000, this);
    private DiaPreviewWindow parent;

    public void imageSelected(int pos) {
        if(pos!=-1){
            currentImage = pos;
            try{img = ImageIO.read(gui.getOneRoseImageList().getImage(currentImage));}catch(Exception ex){}
            mousePosition = ThingWhereTheMouseIsOver.Nothing;
            repaint();
        }
    }

    public void imageAdded(File i) {
        
    }

    public void imageRemoved(File i) {
        
    }

    public void imageHovered(int pos) {
        
    }

    @Override
    public void imageReplaced(File Old, File New, int pos) {
        
    }

    @Override
    public void titleChanged(String Old, String New, int pos) {
        
    }


    public enum ThingWhereTheMouseIsOver{
        Panel,
        Play,
        Pause,
        Next,
        Previous,
        Close,
        Nothing
    }
}
